package com.gutai.admin.dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.gutai.admin.common.util.CollectionUtil;
import com.gutai.admin.common.util.JdbcTempleMapperUtil;
import com.gutai.admin.dao.ICommonDao;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@Repository
@Transactional
public class CommonDao implements ICommonDao {
    private static final Logger logger = LoggerFactory.getLogger(CommonDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public <T> List<T> queryList(String sql, Class<T> clazz) {
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(clazz));
        // try {
        // return JdbcUtil.queryStatement(sql, clazz);
        // } catch (Exception e) {
        // logger.error("query statement error --{}", e);
        // }
        // return Collections.emptyList();
    }

    @Override
    public <T> List<T> queryListByPage(Class<T> clazz, Set<String> feilds, int pageIdx, int pageSize) {
        String sql = JdbcTempleMapperUtil.covertSelectSql(clazz, feilds, null, pageIdx, pageSize);
        logger.debug("Query list ByPage sql: {}", sql);
        return queryList(sql, clazz);
    }

    @Override
    public <T> List<T> queryListByPage(Class<T> clazz, Map<String, Object> fieldsMap, int pageIdx, int pageSize) {
        String sql = JdbcTempleMapperUtil.covertSelectSql(clazz, fieldsMap);
        sql = JdbcTempleMapperUtil.covertSelectByPage(sql, pageIdx, pageSize);
        logger.debug("Query list ByPage sql: {}", sql);
        return queryList(sql, clazz);
    }
    
    @Override
    public <T> List<T> queryListByPage(Class<T> clazz, int pageIdx, int pageSize) {
        String sql = JdbcTempleMapperUtil.covertSelectAllSql(clazz, pageIdx, pageSize);
        logger.debug("Query list ByPage sql: {}", sql);
        return queryList(sql, clazz);
    }

    @Override
    public <T> List<T> queryAllList(Class<T> clazz) {
        String sql = JdbcTempleMapperUtil.covertSelectAllSql(clazz);
        logger.debug("Query list sql: {}", sql);
        return queryList(sql, clazz);
    }

    @Override
    public <T> T queryById(Class<T> clazz, int id) {
        String sql = JdbcTempleMapperUtil.covertSelectSql(clazz, id);
        logger.debug("query byId sql: {}", sql);
        List<T> list = queryList(sql, clazz);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public <T> List<T> queryByConds(Class<T> clazz, Map<String, Object> fieldsMap) {
        String sql = JdbcTempleMapperUtil.covertSelectSql(clazz, fieldsMap);
        logger.debug("queryByConds sql: {}", sql);
        return queryList(sql, clazz);
    }

    @Override
    public <T> int add(T entity) {
        String sql = JdbcTempleMapperUtil.covertInsertSql(entity);
        logger.debug("Add sql: {}", sql);
        return jdbcTemplate.update(sql);
        // try {
        // return JdbcUtil.updateStatement(sql);
        // } catch (Exception e) {
        // logger.error("update statement error --{}", e);
        // }
        // return 0;
    }

    @Override
    public <T> int add(List<T> list) {
        int count = 0;
        for (T data : list) {
            String sql = JdbcTempleMapperUtil.covertInsertSql(data);
            logger.debug("Add sql: {}", sql);
            count += jdbcTemplate.update(sql);
        }
        return count;
    }

    @Override
    public <T> int update(T entity) {
        String sql = JdbcTempleMapperUtil.covertUpdateSql(entity);
        logger.debug("Update sql: {}", sql);
        return jdbcTemplate.update(sql);
        // try {
        // return JdbcUtil.updateStatement(sql);
        // } catch (Exception e) {
        // logger.error("update statement error --{}", e);
        // }
        // return 0;
    }

    @Override
    public <T> int addOrUpdate(T entity) {
        Integer idValue = JdbcTempleMapperUtil.getIdValue(entity);
        T oldData = (T) queryById(entity.getClass(), idValue);
        String sql = "";
        if (oldData != null) { // 修改
            sql = JdbcTempleMapperUtil.covertUpdateSql(entity);
            logger.debug("addOrUpdate(Update) sql: {}", sql);
        } else { // 新增
            sql = JdbcTempleMapperUtil.covertInsertSql(entity);
            logger.debug("addOrUpdate(add) sql: {}", sql);
        }
        return jdbcTemplate.update(sql);
    }

    @Override
    public <T> int delete(Class<T> clazz, int id) {
        String sql = JdbcTempleMapperUtil.covertDeleteSql(clazz, id);
        logger.debug("Delete sql: {}", sql);
        return jdbcTemplate.update(sql);
        // try {
        // return JdbcUtil.updateStatement(sql);
        // } catch (Exception e) {
        // logger.error("update statement error --{}", e);
        // }
        // return 0;
    }

    @Override
    public <T> int delete(Class<T> clazz, Map<String, Object> fieldsMap) {
        String sql = JdbcTempleMapperUtil.covertDeleteSql(clazz, fieldsMap);
        logger.debug("Delete sql: {}", sql);
        return jdbcTemplate.update(sql);
    }

}
